import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:study_demo/base/BaseController.dart';

class CardFlipController extends BaseController
    with GetSingleTickerProviderStateMixin {
  late AnimationController animationController;
  late Animation<double> animation;
  double _currentAngle = 0.0; // 当前旋转角度

  final direction = 0.obs;
  final directions = ['正面', '反面'];

  final type = 0.obs;
  final types = ['卡片', '图片'];

  final effect = "Curves.easeInOut".obs;

  final effects = {
    "Curves.linear": Curves.linear,
    "Curves.easeIn": Curves.easeIn,
    "Curves.easeOut": Curves.easeOut,
    "Curves.easeInOut": Curves.easeInOut,
    "Curves.linearToEaseOut": Curves.linearToEaseOut,
    "Curves.easeInToLinear": Curves.easeInToLinear,
    "Curves.easeInOutBack": Curves.easeInOutBack,
    "Curves.easeInBack": Curves.easeInBack,
    "Curves.easeOutBack": Curves.easeOutBack,
    "Curves.easeInOutCirc": Curves.easeInOutCirc,
    "Curves.easeInCirc": Curves.easeInCirc,
    "Curves.easeOutCirc": Curves.easeOutCirc,
    "Curves.easeInOutCubic": Curves.easeInOutCubic,
    "Curves.easeInCubic": Curves.easeInCubic,
    "Curves.easeOutCubic": Curves.easeOutCubic,
    "Curves.easeInOutCubicEmphasized": Curves.easeInOutCubicEmphasized,
    "Curves.easeInOutExpo": Curves.easeInOutExpo,
    "Curves.easeInExpo": Curves.easeInExpo,
    "Curves.easeOutExpo": Curves.easeOutExpo,
    "Curves.easeInOutQuad": Curves.easeInOutQuad,
    "Curves.easeInQuad": Curves.easeInQuad,
    "Curves.easeOutQuad": Curves.easeOutQuad,
    "Curves.easeInOutQuart": Curves.easeInOutQuart,
    "Curves.easeInQuart": Curves.easeInQuart,
    "Curves.easeOutQuart": Curves.easeOutQuart,
    "Curves.easeInOutQuint": Curves.easeInOutQuint,
    "Curves.easeInQuint": Curves.easeInQuint,
    "Curves.easeOutQuint": Curves.easeOutQuint,
    "Curves.easeInOutSine": Curves.easeInOutSine,
    "Curves.easeInSine": Curves.easeInSine,
    "Curves.easeOutSine": Curves.easeOutSine,
    "Curves.bounceInOut": Curves.bounceInOut,
    "Curves.slowMiddle": Curves.slowMiddle,
    "Curves.decelerate": Curves.decelerate,
  };

  @override
  void onInit() {
    super.onInit();

    animationController = AnimationController(
      duration: Duration(milliseconds: 1200),
      vsync: this,
    );

    animation = Tween(begin: 0.0, end: 3.14159).animate(CurvedAnimation(
      parent: animationController,
      curve: effects[effect]!,
    ));
  }

  changeEffect(String effect) {
    this.effect.value = effect;
    print('effect:${effects[effect]!}');
    animationController.reset();
    _currentAngle = 0;
  }

  flipCard() {
    final startAngle = _currentAngle;
    final endAngle = _currentAngle +
        (direction.value == 0 ? 3.14159 : -3.14159); // 顺时针增加 π 弧度

    animation = Tween(begin: startAngle, end: endAngle).animate(CurvedAnimation(
      parent: animationController,
      curve: effects[effect]!,
    ));


    animationController
        ..reset()..forward();


    _currentAngle = endAngle % (2 * 3.14159); // 保持角度在 [0, 2π] 范围内
  }

  changeType(int value) {
    type.value = value;
    animationController.reset();
    _currentAngle = 0;
  }

  changeDirection(int value) {
    direction.value = value;
    animationController.reset();
    _currentAngle = 0;
  }

  @override
  void onClose() {
    animationController.dispose();
    super.onClose();
  }
}
